package com.isoft.dailymanprovidor.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.isoft.dailymanprovidor.bean.ResponseData;
import com.isoft.dailymanprovidor.bean.ResultCode;
import com.isoft.dailymanprovidor.dao.MeetingRoomDao;
import com.isoft.dailymanprovidor.entity.MeetingRoom;
import com.isoft.dailymanprovidor.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MeetingRoomService {
    @Autowired
    MeetingRoomDao meetingRoomDao;
    //分页获取全部会议室信息
    public ResponseData getallmeeting(Integer currentPage, Integer size){
        ResponseData responseData = new ResponseData();
        Page<MeetingRoom> meetingRoomPage = new Page<>();
        // 初始化分页参数
        if (currentPage == null){
            currentPage = 1;
        }
        if (size == null){
            size =10;
        }
        //构造查询条件
        meetingRoomPage.setCurrent(currentPage).setSize(size);
        QueryWrapper wrapper=new QueryWrapper();
        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(meetingRoomDao.selectPage(meetingRoomPage,wrapper));
        return responseData;
    }
    /*分页获取会议室信息*/
    public ResponseData getallmeetingroom(Integer currentPage, Integer size, String name, String number){
        ResponseData responseData = new ResponseData();
        Page<MeetingRoom> meetingRoomPage = new Page<>();
        // 初始化分页参数
        if (currentPage == null){
            currentPage = 1;
        }
        if (size == null){
            size =10;
        }
        //构造查询条件
        meetingRoomPage.setCurrent(currentPage).setSize(size);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        wrapper.like("number",number);
        wrapper.orderByAsc("id");

        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(meetingRoomDao.selectPage(meetingRoomPage,wrapper));
        return responseData;
    }

    //验证会议室名称是否重复
    public boolean meetingroonExist(String name){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",name);
        MeetingRoom existMeetingRoon = meetingRoomDao.selectOne(wrapper);
        if (existMeetingRoon != null){
            return true;
        }
        return false;
    }
    //修改会议室信息
    public boolean updatemeetingroom(MeetingRoom meetingRoom){
        UpdateWrapper<MeetingRoom> wrapper=new UpdateWrapper<>();
        wrapper.set("name",meetingRoom.getName());
        wrapper.set("number",meetingRoom.getNumber());
        wrapper.set("facilities",meetingRoom.getFacilities());
        wrapper.set("location",meetingRoom.getLocation());
        wrapper.set("capacity",meetingRoom.getCapacity());
        wrapper.set("notes",meetingRoom.getNotes());
        wrapper.eq("id",meetingRoom.getId());
        return meetingRoomDao.update(wrapper)>0;
    }
    //修改会议室状态
    public boolean updatestatus(MeetingRoom meetingRoom){
       return meetingRoomDao.updatastatus(meetingRoom)>0;
    }

    //新增会议室
    public boolean addmeetingroom(MeetingRoom meetingRoom){

        if (meetingRoom.getName().isEmpty() || meetingRoom.getName()== null ){
            log.error("会议室名称不能为空");
            return false;
        } else if (meetingRoom.getNumber().isEmpty() || meetingRoom.getNumber() == null) {
            log.error("会议编号为空");
            return false;
        } else if (meetingRoom.getLocation().isEmpty() || meetingRoom.getLocation() == null) {
            log.error("会议室位置为空");
            return false;
        } else if (meetingRoom.getCapacity()==null || !IdUtil.idCheck(meetingRoom.getCapacity())) {
            log.error("容纳总数为空");
            return false;
        }
        return meetingRoomDao.Addmeetingroom(meetingRoom)>0;
    }
    //删除会议室信息
    public boolean deletemeetingroom(Integer id){
        return meetingRoomDao.deleteById(id)>0;
    }
}
